let Level = require('Level');
let soundMgr = require('soundMgr');
let dragons = require('dragons');
let nodeMgr = require('nodeMgr');
let sounds = require('sounds');
let DragLayer = require('dragLayer');
let DragTips = require('DragTips');
let yySpine = require('yySpine');
let actions = require('actions');
cc.Class({
    extends: Level,

    properties: {
        nextLevelId:2,
        isNow:0,

        ziDan:cc.Prefab,
        feiJi:cc.Node,
        zidan3:cc.Node,
        zidanqu:cc.Node,
        guaiwuList:cc.Node,
        answerList:cc.Node,
        yun:cc.Node,
        guaiwuDie:0,
        zidanList:[cc.Prefab],
        feitime:1,
        juli:1000,
        nowTime:-1,
        shouzhi:cc.Node,
    },
    start () {

    },
    /**
     * 开始关卡
     */
    startLevel(){
        let self = this;
        nodeMgr.enableTouch();
        self._nextQuestion();
        window.canMove = -1;
    },

    _nextQuestion(){
        let self = this;
        if(self.isNow < 1){
            self.guaiwuList.children[self.isNow].active = true;
            soundMgr.playSoundEffectByName("xiazhui");
            actions.moveTo(self.guaiwuList.children[self.isNow],2,0,self.guaiwuList.children[self.isNow].y,function () {
                yySpine.playAnimation(self.feiJi,"chijing",1,true,function () {
                    self.answerList.children[self.isNow].active = true;
                    yySpine.playAnimation(self.feiJi,"idle",1,true,function () {
                        yySpine.playAnimation(self.feiJi,"idle",0,true)
                    })
                })
                soundMgr.playSoundEffectByName("zshuxue11_01",function () {
                    soundMgr.playSoundEffectByName("zshuxue11_02");
                    self.nowTime = (new Date()).getTime();
                    nodeMgr.enableTouch();
                });
            })
        }else {
            self.guaiwuList.children[self.isNow].active = true;
            self.nowTime = (new Date()).getTime();
            soundMgr.playSoundEffectByName("xiazhui");
            actions.moveTo(self.guaiwuList.children[self.isNow],2,0,self.guaiwuList.children[self.isNow].y,function () {
                nodeMgr.enableTouch();
                self.answerList.children[self.isNow].active = true;
                yySpine.playAnimation(self.feiJi,"idle",0,true)
                switch (self.isNow) {
                    case 1:
                        soundMgr.playSoundEffectByName("zshuxue11_03");
                        break;
                    case 2:
                        soundMgr.playSoundEffectByName("zshuxue11_02");
                        break;
                    case 3:
                        soundMgr.playSoundEffectByName("zshuxue11_03");
                        break;
                    case 4:
                        soundMgr.playSoundEffectByName("zshuxue11_04");
                        break;
                    case 5:
                        soundMgr.playSoundEffectByName("zshuxue11_05");
                        break;
                    case 6:
                        soundMgr.playSoundEffectByName("zshuxue11_04");
                        break;
                    case 7:
                        soundMgr.playSoundEffectByName("zshuxue11_05");
                        break;
                }

            })
        }
    },


    beginGame(){
        let self = this;
        if(self.isNow > 0){
            nodeMgr.enableTouch();
            window.canMove = self.isNow;
            self.schedule(function(){
                switch (self.isNow) {
                    case 0:
                        soundMgr.playSoundEffectByName("biu");
                        self.shengchengzidan1();
                        break;
                    case 1:
                        soundMgr.playSoundEffectByName("biu");
                        self.shengchengzidan1();
                        break;
                    case 2:
                        soundMgr.playSoundEffectByName("biu");
                        self.shengchengzidan1();
                        self.shengchengzidan2();
                        break;
                    case 3:
                        soundMgr.playSoundEffectByName("biu");
                        self.shengchengzidan1();
                        self.shengchengzidan2();
                        break;
                    case 4:
                        soundMgr.playSoundEffectByName("biu");
                        self.shengchengzidan1();
                        self.shengchengzidan2();
                        self.shengchengzidan3();
                        break;
                    case 5:
                        soundMgr.playSoundEffectByName("biu");
                        self.shengchengzidan1();
                        self.shengchengzidan2();
                        self.shengchengzidan3();
                        break;
                    case 6:
                        soundMgr.playSoundEffectByName("biu");
                        self.shengchengzidan1();
                        self.shengchengzidan2();
                        self.shengchengzidan3();
                        self.shengchengzidan4();
                        break;
                    case 7:
                        soundMgr.playSoundEffectByName("biu");
                        self.shengchengzidan1();
                        self.shengchengzidan2();
                        self.shengchengzidan3();
                        self.shengchengzidan4();
                        break;
                }
            },0.2);
        }else {
            self.feiJi.children[1].active = true;
            self.feiJi.children[0].active = true;
            self.shouzhi.active = true;
            actions.moveTo(self.shouzhi,1,self.shouzhi.x,100,function () {
                self.shouzhi.y = -120;
                actions.moveTo(self.shouzhi,1,self.shouzhi.x,-330,function () {
                    self.feiJi.children[1].active = false;
                    self.feiJi.children[0].active = false;
                    self.shouzhi.active = false;
                });
            });
            soundMgr.playSoundEffectByName("zshuxue11_06",function () {
                window.canMove = self.isNow;
                self.schedule(function(){
                    soundMgr.playSoundEffectByName("biu");
                    self.shengchengzidan1();
                },0.2)
                nodeMgr.enableTouch();
            });
        }

    },


    hedan(){
        let self = this;
        console.log("核弹");
    },
    jiaxue(){
        let self = this;
        // console.log("加血");
    },
    guaiwu(){
        let self = this;
        self.guaiwuDie++;
        console.log(self.guaiwuDie);
        if(self.guaiwuDie == self.guaiwuList.children[self.isNow].children.length){
            nodeMgr.disableTouch();
            self.unscheduleAllCallbacks(self);
            self.feijituichang();
            soundMgr.playSoundEffectByName("tongyong_bigright");
            
        }
    },


    feijituichang(){
        let self = this;
        actions.moveTo(self.feiJi,3,904,self.feiJi.y,function () {
            yySpine.playAnimation(self.feiJi,"idle",0,true)
            self.isNow++;
            if(self.isNow == 8){
                // soundMgr.playSoundEffectByName("tongyong_bigright");
                self._gameOver();
                return;
            }else {
                switch (self.isNow) {
                    case 2:
                        yySpine.setSpineSkin(self.feiJi,"2ji");
                        break;
                    case 4:
                        yySpine.setSpineSkin(self.feiJi,"3ji");
                        break;
                    case 6:
                        yySpine.setSpineSkin(self.feiJi,"4ji");
                        break;
                }

                actions.moveTo(cc.Camera.main.node,2,1334,0,function () {
                    cc.Camera.main.node.x = 0;
                    self.feiJi.x = -430;
                    self.guaiwuDie = 0;
                    self._nextQuestion();
                });
            }
        });
        soundMgr.playSoundEffectByName("chongci");
        yySpine.playAnimation(self.feiJi,"chongci",1,true,function () {
            yySpine.playAnimation(self.feiJi,"idle",0,true)
        })
    },
    feiji(){
        let self = this;
        // console.log("飞机");
    },
    zidan(){
        let self = this;
        // console.log("子弹");
    },

    shengchengzidan1(){
        let self = this;
        var item = cc.instantiate(self.zidanList[self.isNow]);
        self.zidanqu.addChild(item);
        item.x = self.feiJi.x + 90;
        item.y = self.feiJi.y - 50;
        item.scaleX = 0.6;
        item.scaleY = 0.6;
        item.active = true;
        actions.moveTo(item,this.feitime,this.juli,item.y,function () {
            item.active = false;
            item.destroy();
        });
    },

    shengchengzidan2(){
        let self = this;
        var item = cc.instantiate(self.zidanList[self.isNow]);
        self.zidanqu.addChild(item);
        item.x = self.feiJi.x + 65;
        item.y = self.feiJi.y - 58;
        item.scaleX = 0.6;
        item.scaleY = 0.6;
        item.active = true;
        actions.moveTo(item,this.feitime,this.juli,item.y + 80,function () {
            item.active = false;
            item.destroy();
        });
    },

    shengchengzidan3(){
        let self = this;
        var item = cc.instantiate(self.zidanList[self.isNow]);
        self.zidanqu.addChild(item);
        item.x = self.feiJi.x + 40;
        item.y = self.feiJi.y - 40;
        item.scaleX = 0.6;
        item.scaleY = 0.6;
        item.active = true;
        actions.moveTo(item,this.feitime,this.juli,item.y -80,function () {
            item.active = false;
            item.destroy();
        });
    },

    shengchengzidan4(){
        let self = this;
        var item = cc.instantiate(self.zidanList[self.isNow]);
        self.zidanqu.addChild(item);
        item.x = self.feiJi.x + 20;
        item.y = self.feiJi.y - 55;
        item.scaleX = 0.6;
        item.scaleY = 0.6;
        item.active = true;
        actions.moveTo(item,this.feitime,this.juli,item.y - 120,function () {
            item.active = false;
            item.destroy();
        });
    },

    onClick(data){
        let self = this;
        nodeMgr.disableTouch();
        cc.audioEngine.pauseAll();
        self.nowTime = -1;
        let target = data.target;
        target.node.children[0].active = true;
        self.scheduleOnce(function(){
            target.node.children[0].active = false;
        },0.5);
        if(target.node.name == "right"){
            soundMgr.playSoundEffectByName("tongyong_right");
            self.rightAnswerShow(target);
        }else {
            self.nowTime = (new Date()).getTime();
            soundMgr.playSoundEffectByName("tongyong_error");
            actions.shake(target.node,0.2);
            nodeMgr.enableTouch();
        }
    },

    rightAnswerShow(target){
        let self = this;
        switch (self.isNow) {
            case 0:
                for(var i = 2;i < target.node._parent.children.length;i++){
                    if(target.node._parent.children[i].name != "right"){
                        target.node._parent.children[i].active = false;
                    }
                }
                actions.moveTo(target.node,0.5,0,target.node.y,function () {
                    actions.scaleTo(target.node,0.5,1.1,1.1,function () {
                        actions.scaleTo(target.node,0.5,0.9,0.9,function () {
                            actions.scaleTo(target.node,0.5,1.1,1.1,function () {
                                actions.scaleTo(target.node,0.5,0.9,0.9,function () {
                                    target.node._parent.active = false;
                                    self.beginGame();
                                })
                            })
                        })
                    })
                })
                break;
            case 1:
                for(var i = 2;i < target.node._parent.children.length;i++){
                    if(target.node._parent.children[i].name != "right"){
                        target.node._parent.children[i].active = false;
                    }
                }
                actions.moveTo(target.node,0.5,0,target.node.y,function () {
                    actions.scaleTo(target.node,0.5,1.1,1.1,function () {
                        actions.scaleTo(target.node,0.5,0.9,0.9,function () {
                            actions.scaleTo(target.node,0.5,1.1,1.1,function () {
                                actions.scaleTo(target.node,0.5,0.9,0.9,function () {
                                    target.node._parent.active = false;
                                    self.beginGame();
                                })
                            })
                        })
                    })
                })
                break;
            case 2:
                for(var i = 2;i < target.node._parent.children.length;i++){
                    if(target.node._parent.children[i].name != "right"){
                        target.node._parent.children[i].active = false;
                    }
                }
                actions.moveTo(target.node,0.5,0,target.node.y,function () {
                    actions.scaleTo(target.node,0.5,1.1,1.1,function () {
                        actions.scaleTo(target.node,0.5,0.9,0.9,function () {
                            actions.scaleTo(target.node,0.5,1.1,1.1,function () {
                                actions.scaleTo(target.node,0.5,0.9,0.9,function () {
                                    target.node._parent.active = false;
                                    self.beginGame();
                                })
                            })
                        })
                    })
                })
                break;
            case 3:
                for(var i = 2;i < target.node._parent.children.length;i++){
                    if(target.node._parent.children[i].name != "right"){
                        target.node._parent.children[i].active = false;
                    }
                }
                actions.moveTo(target.node,0.5,0,target.node.y,function () {
                    actions.scaleTo(target.node,0.5,1.1,1.1,function () {
                        actions.scaleTo(target.node,0.5,0.9,0.9,function () {
                            actions.scaleTo(target.node,0.5,1.1,1.1,function () {
                                actions.scaleTo(target.node,0.5,0.9,0.9,function () {
                                    target.node._parent.active = false;
                                    self.beginGame();
                                })
                            })
                        })
                    })
                })
                break;
            case 4:
                for(var i = 4;i < target.node._parent.children.length;i++){
                    if(target.node._parent.children[i].name != "right"){
                        target.node._parent.children[i].active = false;
                    }
                }
                actions.moveTo(target.node._parent.children[2],0.5,target.node._parent.children[2].x,-70)
                actions.moveTo(target.node,0.5,10,110,function () {
                    actions.moveTo(target.node,0.5,10,-11,function () {
                        target.node._parent.children[3].active = true;
                        yySpine.playAnimation(target.node._parent.children[3].children[0],"animation",1,true,function () {
                            yySpine.playAnimation(target.node._parent.children[3].children[0],"animation",1,true,function () {
                                target.node.active = false;
                                target.node._parent.children[2].active = false;
                                target.node._parent.children[3].active = false;
                                // target.node._parent.children[5].active = true;
                                // actions.scaleTo(target.node._parent.children[5],0.5,1.5,1.5,function () {
                                //     actions.scaleTo(target.node._parent.children[5],0.5,1.3,1.3,function () {
                                //         actions.scaleTo(target.node._parent.children[5],0.5,1.5,1.5,function () {
                                //             actions.scaleTo(target.node._parent.children[5],0.5,1.3,1.3,function () {
                                                target.node._parent.active = false;
                                                self.beginGame();
                                            // })
                                        // })
                                    // })
                                // })
                            })
                        })
                    })
                })
                break;
            case 5:
                for(var i = 3;i < target.node._parent.children.length;i++){
                    if(target.node._parent.children[i].name != "right"){
                        target.node._parent.children[i].active = false;
                    }
                }
                actions.moveTo(target.node._parent.children[4],0.5,80,-40)
                actions.moveTo(target.node,0.5,-80,-40,function () {
                    actions.moveTo(target.node._parent.children[4],0.5,35,-40)
                    actions.moveTo(target.node,0.5,-11,-40,function () {
                        target.node._parent.children[3].active = true;
                        yySpine.playAnimation(target.node._parent.children[3].children[0],"animation",1,true,function () {
                            yySpine.playAnimation(target.node._parent.children[3].children[0],"animation",1,true,function () {
                                target.node.active = false;
                                target.node._parent.children[3].active = false;
                                target.node._parent.children[4].active = false;
                                // target.node._parent.children[5].active = true;
                                // actions.scaleTo(target.node._parent.children[5],0.5,1.1,1.1,function () {
                                //     actions.scaleTo(target.node._parent.children[5],0.5,0.9,0.9,function () {
                                //         actions.scaleTo(target.node._parent.children[5],0.5,1.1,1.1,function () {
                                //             actions.scaleTo(target.node._parent.children[5],0.5,0.9,0.9,function () {
                                                target.node._parent.active = false;
                                                self.beginGame();
                                            // })
                                        // })
                                    // })
                                // })
                            });
                        });

                        // target.node.active = false;
                        // target.node._parent.children[3].active = false;
                        // target.node._parent.children[4].active = true;
                        // actions.scaleTo(target.node._parent.children[4],0.5,1.2,1.2,function () {
                        //     actions.scaleTo(target.node._parent.children[4],0.5,1,1,function () {
                        //         actions.scaleTo(target.node._parent.children[4],0.5,1.2,1.2,function () {
                        //             actions.scaleTo(target.node._parent.children[4],0.5,1,1,function () {
                        //                 target.node._parent.active = false;
                        //                 self.beginGame();
                        //             })
                        //         })
                        //     })
                        // })
                    })
                })
                break;
            case 6:
                for(var i = 3;i < target.node._parent.children.length;i++){
                    if(target.node._parent.children[i].name != "right"){
                        target.node._parent.children[i].active = false;
                    }
                }
                actions.moveTo(target.node._parent.children[2],0.5,-5,-20)
                actions.moveTo(target.node,0.5,50,-20,function () {
                    target.node._parent.children[3].active = true;
                    yySpine.playAnimation(target.node._parent.children[3].children[0],"animation",1,true,function () {
                        yySpine.playAnimation(target.node._parent.children[3].children[0],"animation",1,true,function () {
                            target.node.active = false;
                            target.node._parent.children[2].active = false;
                            // target.node._parent.children[4].active = true;
                            // actions.scaleTo(target.node._parent.children[4],0.5,1.5,1.5,function () {
                            //     actions.scaleTo(target.node._parent.children[4],0.5,1.3,1.3,function () {
                            //         actions.scaleTo(target.node._parent.children[4],0.5,1.5,1.5,function () {
                            //             actions.scaleTo(target.node._parent.children[4],0.5,1.3,1.3,function () {
                                            target.node._parent.active = false;
                                            self.beginGame();
                                        // })
                                    // })
                                // })
                            // })
                        })
                    })
                })
                break;
            case 7:
                for(var i = 3;i < target.node._parent.children.length;i++){
                    if(target.node._parent.children[i].name != "right"){
                        target.node._parent.children[i].active = false;
                    }
                }
                actions.moveTo(target.node._parent.children[4],0.5,20,-20)
                actions.moveTo(target.node,0.5,20,-68,function () {
                    target.node._parent.children[3].active = true;
                    yySpine.playAnimation(target.node._parent.children[3].children[0],"animation",1,true,function () {
                        yySpine.playAnimation(target.node._parent.children[3].children[0],"animation",1,true,function () {
                            target.node._parent.children[4].active = false;
                            target.node._parent.children[3].active = false;
                            target.node.active = false;
                            // target.node._parent.children[5].active = true;
                            // actions.scaleTo(target.node._parent.children[5],0.5,1.2,1.2,function () {
                            //     actions.scaleTo(target.node._parent.children[5],0.5,1,1,function () {
                            //         actions.scaleTo(target.node._parent.children[5],0.5,1.2,1.2,function () {
                            //             actions.scaleTo(target.node._parent.children[5],0.5,1,1,function () {
                                            target.node._parent.active = false;
                                            self.beginGame();
                                        // })
                                    // })
                                // })
                            // })
                        })
                    })
                })
                break;
        }

    },

    update (dt) {
        let self = this;
        if(this.nowTime > 0){
            if(this.nowTime + 20000 < (new Date()).getTime()){
                switch (self.isNow) {
                    case 0:
                        soundMgr.playSoundEffectByName("zshuxue11_02");
                        break;
                    case 1:
                        soundMgr.playSoundEffectByName("zshuxue11_03");
                        break;
                    case 2:
                        soundMgr.playSoundEffectByName("zshuxue11_02");
                        break;
                    case 3:
                        soundMgr.playSoundEffectByName("zshuxue11_03");
                        break;
                    case 4:
                        soundMgr.playSoundEffectByName("zshuxue11_04");
                        break;
                    case 5:
                        soundMgr.playSoundEffectByName("zshuxue11_05");
                        break;
                    case 6:
                        soundMgr.playSoundEffectByName("zshuxue11_04");
                        break;
                    case 7:
                        soundMgr.playSoundEffectByName("zshuxue11_05");
                        break;
                }
                this.nowTime = (new Date()).getTime() + 6000;
            }
        }
    },
});
